﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.IO;
using TFMAPI;

namespace TwitterFollowersMonitor
{

    /// <summary>
    /// Holds all the base information for the application to work
    /// </summary>
    public class TwitterMonitorizedAccountsInfo
    {
        //The name of the application (used as the name of the folder to store data for the user)
        public const string APP_NAME_FOLDER = "TwitterFollowersMonitor";
        //The name of the file containing the information for this class
        public const string FILE_DATABASE_NAME = @"TFMData.json";

        //The list of accounts to monitorize
        public Dictionary<String, TwitterUserInfo> Accounts = new Dictionary<string, TwitterUserInfo>();

        /// <summary>
        /// Gets the data file path ensuring that the application folder exists (creating it if not).
        /// </summary>
        /// <returns></returns>
        private static string GetUserDataFilePath()
        {
            string userDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            userDataPath = Path.Combine(userDataPath, APP_NAME_FOLDER); //Add the application folder to the path
            //Check if App folder exists
            if (!Directory.Exists(userDataPath))
                Directory.CreateDirectory(userDataPath);

            return Path.Combine(userDataPath, FILE_DATABASE_NAME);
        }

        /// <summary>
        /// Static Factory method that loads the accounts information from disk into a TwitterMonitorizedAccountsInfo object instance
        /// </summary>
        /// <returns></returns>
        public static TwitterMonitorizedAccountsInfo Load()
        {
            string jsonAccounts = File.ReadAllText(GetUserDataFilePath());
            return jsonAccounts.json2Object<TwitterMonitorizedAccountsInfo>();
        }

        /// <summary>
        /// This method persists to disk the current account information, overwriting the existing one if any
        /// </summary>
        public void Save()
        {
            string myJson = this.object2Json<TwitterMonitorizedAccountsInfo>();
            File.WriteAllText(GetUserDataFilePath(), myJson);
        }

    }
}
